package cn.stylefeng.guns.modular.api.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.api.entity.Check;
import cn.stylefeng.guns.modular.api.entity.Materiel;
import cn.stylefeng.guns.modular.api.mapper.CheckMapper;
import cn.stylefeng.guns.modular.api.model.params.CheckParam;
import cn.stylefeng.guns.modular.api.model.params.MaterielParam;
import cn.stylefeng.guns.modular.api.model.result.CheckResult;
import  cn.stylefeng.guns.modular.api.service.CheckService;
import cn.stylefeng.guns.modular.api.service.MaterielService;
import cn.stylefeng.guns.modular.api.vo.CheckStatusEnum;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 盘点 服务实现类
 * </p>
 *
 * @author wusonghe
 * @since 2019-08-31
 */
@Service
public class CheckServiceImpl extends ServiceImpl<CheckMapper, Check> implements CheckService {

    @Autowired
    private MaterielService materielService;

    @Override
    @Transactional
    public void add(CheckParam param) throws ServiceException{

        //盘点记录
        Check entity = getEntity(param);
        this.save(entity);

        //物料抵扣
        Integer materielId = param.getMaterielId();
        Materiel materiel = materielService.getById(materielId);
        MaterielParam materielParam = new MaterielParam();
        if (param.getStatus() == CheckStatusEnum.SUB.getCode().intValue()) {
            Integer number = materiel.getNumber() - param.getNumber();
            if (number <= 0){
                throw new ServiceException(500,"物料数量不足抵扣");
            }
            materielParam.setNumber(number);
        }else {
            Integer unuseNumber = materiel.getUnuseNumber() + param.getNumber();
            materielParam.setUnuseNumber(unuseNumber);
        }

        materielParam.setMaterielId(materielId);
        materielService.update(materielParam);

    }

    @Override
    public void delete(CheckParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CheckParam param){
        Check oldEntity = getOldEntity(param);
        Check newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CheckResult findBySpec(CheckParam param){
        return null;
    }

    @Override
    public List<CheckResult> findListBySpec(CheckParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CheckParam param){
        Page pageContext = getPageContext();
        if (pageContext.getSize()==0) {
            List datas =  this.baseMapper.customList(param);
            return LayuiPageFactory.createDataInfo(datas);
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CheckParam param){
        return param.getCheckId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Check getOldEntity(CheckParam param) {
        return this.getById(getKey(param));
    }

    private Check getEntity(CheckParam param) {
        Check entity = new Check();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
